package ex.encapsulation;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * 套娃
 * @author small瑞
 */
public class Decimal {

    private final long value;

    public Decimal(long value){
        this.value = value;
    }

    /**
     * 转化为二进制
     * @return
     */
    public String toBinaryString(){
        StringBuilder sb = new StringBuilder();
        long temp = this.value;
        while(temp!=0){
            int cur = (int) (temp%2);
            sb.append(cur);
            temp/=2;
        }
//        String format = "00000000000000000000000000000000";
//        System.out.println(format.length());
//        return sb.toString();
        String format = "00000000000000000000000000000000";
        DecimalFormat decimalFormat = new DecimalFormat(format);
        return decimalFormat.format(Long.parseLong(sb.reverse().toString()));
    }
    public String toHexString(){
        StringBuilder sb = new StringBuilder();
        long temp = this.value;
        while(temp!=0){
            int cur = (int) (temp%16);
            if(cur<10){
                sb.append(cur);
            }
            if(cur>=10){
                switch (cur){
                    case 10->sb.append("A");
                    case 11->sb.append("B");
                    case 12->sb.append("C");
                    case 13->sb.append("D");
                    case 14->sb.append("E");
                    case 15->sb.append("F");
                    default -> throw new RuntimeException("错误数据!");
                }
            }
            temp/=16;
        }
        return sb.reverse().toString();
    }

    public static void main(String[] args) {
        Decimal decimal = new Decimal(214795);
        System.out.println(decimal.toBinaryString());
        System.out.println(decimal.toHexString());
//        Decimal decimal1 = new Decimal(2147483647);
//        System.out.println(decimal1.toBinaryString());
//        System.out.println(decimal1.toHexString());
    }


}
